﻿//设置弹窗高度
function setHeight(h) {
    var showHeight = $(window).height() - 20;
    return showHeight > h ? h : showHeight;
}

//设置弹窗宽度
function setWidth(w) {
    var showHeight = $(window).width() - 20;
    return showHeight > w ? w : showHeight;
}

//用layer弹出图层
function layerFrame(url, title, w, h, shadeclose) {
    var layerIndex = layer.open({
        type: 2,
        title: title,
        shadeClose: shadeclose,
        skin: 'layui-layer-rim', //加上边框
        area: ['' + setWidth(w) + 'px', '' + setHeight(h) + 'px'], //宽高
        content: url
    });
    return layerIndex;
}

//异步提交成功后的方法
function success(ajaxobj) {
    layer.alert(ajaxobj.msg, {
        skin: 'layui-layer-molv',
        closeBtn: 0,
        icon: ajaxobj.iconid
    }, function (index) {
        //alert(index);
        window.parent.CloseEdit();
        //window.parent.$("#search").click();
    });
}

//分页
function page(dataelem, containerelem, callback) {
    var ajaxobj = JSON.parse($('#' + dataelem).val());
    laypage({
        cont: containerelem, //容器。值支持id名、原生dom对象，jquery对象。【如该容器为】：<div id="page1"></div>
        pages: ajaxobj.pagecount, //通过后台拿到的总页数
        curr: ajaxobj.currpage, //当前页
        jump: callback
    });
}

//获取guid
function getGuid() {
    function S4() {
        return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
    }
    return (S4() + S4() + S4() + S4() + S4() + S4() + S4() + S4());
}

//weui弹窗提示
function showDialog(content, title, btn) {
    if (typeof (title) == 'undefined') {
        title = '系统提示';
    }
    if (typeof (btn) == 'undefined') {
        btn = '确定';
    }
    var guid = getGuid();
    var html = '<div class="weui_dialog_alert" id="' + guid + '"><div class="weui_mask"></div><div class="weui_dialog"><div class="weui_dialog_hd"><strong class="weui_dialog_title" style="color:black">' + title + '</strong></div><div class="weui_dialog_bd">' + content + '</div><div class="weui_dialog_ft"><a href="javascript:void(0)" class="weui_btn_dialog primary" onclick="closeDialog(\'' + guid + '\')">' + btn + '</a></div></div></div>';
    $('body').append(html);
    $('.weui_dialog_alert').show();
    return guid;
}

//weui弹窗提示
function showDialog1111(content, title, btn) {
    if (typeof (title) == 'undefined') {
        title = '系统提示';
    }
    if (typeof (btn) == 'undefined') {
        btn = '确定';
    }
    var guid = getGuid();
    var html = '<div style="display: none;" id="' + guid + '"><div class="weui-mask"></div><div class="weui-dialog"><div class="weui-dialog__hd"><strong class="weui-dialog__title" style="color:black">' + title + '</strong></div><div class="weui-dialog__bd">' + content + '</div><div class="weui-dialog__ft"><a href="javascript:void(0)" class="weui-dialog__btn weui-dialog__btn_primary" onclick="closeDialog(\'' + guid + '\')">' + btn + '</a></div></div></div>';
    $('body').append(html);
    $('#' + guid).show();
    return guid;
}

//weui弹窗提示
function showDialogTwo(content, title, btn) {
    if (typeof (title) == 'undefined') {
        title = '系统提示';
    }
    if (typeof (btn) == 'undefined') {
        btn = '确定';
    }
    var guid = getGuid();
    var html = '<div class="weui_dialog_alert" id="' + guid + '" style="position:absolute;z-index:99999999;"><div class="weui_mask"></div><div class="weui_dialog"><div class="weui_dialog_hd"><strong class="weui_dialog_title" style="color:black">' + title + '</strong></div><div class="weui_dialog_bd">' + content + '</div><div class="weui_dialog_ft"><a href="javascript:void(0)" class="weui_btn_dialog primary" onclick="closeDialog(\'' + guid + '\')">' + btn + '</a></div></div></div>';
    $('body').append(html);
    $('.weui_dialog_alert').show();
    return guid;
}

//微信确认弹窗
function weConfirm(title, content, fn) {
    return { title: title, content: content, fn: fn };
}
//weui弹窗提示
function showConfirm(content, fn, title) {
    if (typeof (title) == 'undefined') {
        title = '系统提示';
    }
    var guid = getGuid();
    var guid2 = getGuid();
    var html = '<div class="weui_dialog_alert" id="' + guid + '"><div class="weui_mask"></div><div class="weui_dialog"><div class="weui_dialog_hd"><strong class="weui_dialog_title">' + title + '</strong></div><div class="weui_dialog_bd">' + content + '</div><div class="weui_dialog_ft"> <a href="javascript:void(0)" class="weui_btn_dialog default" onclick="closeDialog(\'' + guid + '\');">取消</a><a href="javascript:void(0)" id="' + guid2 + '" class="weui_btn_dialog primary">确定</a></div></div></div>';
    $('body').append(html);
    $('.weui_dialog_alert').show();
    $('#' + guid2 + '').click(function () {
        fn();
        closeDialog(guid);
    });

    return guid;
}

//weui弹窗提示
function showConfirmEx(content, fn, title) {
    if (typeof (title) == 'undefined') {
        title = '系统提示';
    }
    var guid = getGuid();
    var guid2 = getGuid();
    var html = '<div class="weui_dialog_confirm" id="' + guid + '"><div class="weui_mask"></div><div class="weui_dialog"><div class="weui_dialog_hd"><strong class="weui_dialog_title">' + title + '</strong></div><div class="weui_dialog_bd">' + content + '</div><div class="weui_dialog_ft"> <a href="javascript:void(0)" class="weui_btn_dialog default" onclick="closeDialog(\'' + guid + '\');">取消</a><a href="javascript:void(0)" id="' + guid2 + '" class="weui_btn_dialog primary">确定</a></div></div></div>';
    $('body').append(html);
    $('.weui_dialog_alert').show();
    $('#' + guid2 + '').click(function () {
        fn();
        closeDialog(guid);
    });

    return guid;
}


//关闭弹窗
function closeDialog(index) {
    $('#' + index + '').remove();
}

//关闭sheet
function CloseSheet(id) {
    $('#' + id + '').hide();
}

//展示sheet
function showSheet(id) {
    $('#' + id + '').show();
}


//同步提交
function successmsg(msg, iconid) {
    layer.alert(msg, {
        skin: 'layui-layer-molv',
        closeBtn: 0,
        icon: iconid
    }, function () {
        //alert(index);
        window.parent.CloseEdit();
        //window.parent.$("#search").click();
    });
}

//-----------验证脚本------------

//是否为空
function Isempty(classid, btnid, index) {
    if (index == "" || index == undefined) {
        index = 2;
    }
    var isok = true;
    var elems = $('.' + classid + '');
    for (var i = 0; i < elems.length; i++) {
        if ($(elems[i]).val() == "" && $(elems[i]).html() == "") {
            var name = $(elems[i]).attr("dataname");
            layer.tips('' + name + '不能为空', '#' + btnid + '', { tips: index });
            isok = false;
            break;
        }
    }
    return isok;
}

//必须为整数
function IsInt(classid, btnid, index) {
    if (index == "" || index == undefined) {
        index = 2;
    }
    var isok = true;
    var elems = $('.' + classid + '');
    var reg = /^\d+$/;
    for (var i = 0; i < elems.length; i++) {
        if (!reg.test($(elems[i]).val())) {
            var name = $(elems[i]).attr("dataname");
            layer.tips('' + name + '必须为正整数', '#' + btnid + '', { tips: index });
            isok = false;
            break;
        }
    }
    return isok;
}

//金额
function IsDecimal(classid, btnid, index) {
    if (index == "" || index == undefined) {
        index = 2;
    }
    var isok = true;
    var elems = $('.' + classid + '');
    var reg = /^(([0-9]|([1-9][0-9]{0,9}))((\.[0-9]{1,2})?))$/;
    for (var i = 0; i < elems.length; i++) {
        if (!reg.test($(elems[i]).val())) {
            var name = $(elems[i]).attr("dataname");
            layer.tips('' + name + '格式不正确', '#' + btnid + '', { tips: index });
            isok = false;
            break;
        }
    }
    return isok;
}

//-----------验证脚本------------